﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public enum WeaponType
{
    Gun,
    Rifle,
    Rocket,
    NullWeapon
}
public abstract class Weapon  {
    protected float m_atk;//攻击力
    protected float m_atkRange;//攻击范围
    protected float m_atkPlusValue;//攻击加成

    protected GameObject m_gameObject;//武器模型
    protected Character m_weaponOwner;//武器持有者


    //Effect 
    protected ParticleSystem m_particlesSystems;
    protected LineRenderer m_lineRenderer;
    protected AudioSource m_Audio;
    protected Light m_Light;

    public Weapon(GameObject gameobject ,float atk,float atkRange,float atkPlusValue) 
    {
        m_gameObject = gameobject;
        m_atk = atk;
        m_atkRange = atkRange;
        m_atkPlusValue = atkPlusValue;

        Transform p = m_gameObject.transform.Find("Effect");
        m_particlesSystems = p.GetComponent<ParticleSystem>();
        m_lineRenderer = p.GetComponent<LineRenderer>();
        m_Audio = p.GetComponent<AudioSource>();
        m_Light = p.GetComponent<Light>();

    }

    public float atkValue { get { return m_atk + m_atkPlusValue; } }

    protected float m_EffectDisplayTime;
    /// <summary>
    /// 获得攻击范围
    /// </summary>
    public float AttackRange
    {
        get { return m_atkRange; }
    }
    public void Fire(Vector3 target)
    {
        DoMuzzleEffect();
        DoBulletEffect(target);
        DoSoundEffect();
    }
    /// <summary>
    /// 播放枪口特效
    /// </summary>
    protected void DoMuzzleEffect()
    {
        m_particlesSystems.Stop();
        m_particlesSystems.Play();
        m_Light.enabled = true;
    }
    /// <summary>
    /// 播放子弹特效 调用ShowBulletEffect
    /// </summary>
    protected abstract void DoBulletEffect(Vector3 target);

    protected void ShowBulletEffect(float width,Vector3 targetPosition)
    {
        m_lineRenderer.enabled = true;
        m_lineRenderer.startWidth = width;
        m_lineRenderer.endWidth = width;
        m_lineRenderer.SetPosition(0, m_gameObject.transform.position);
        m_lineRenderer.SetPosition(1, targetPosition);
    }
    /// <summary>
    /// 播放声音特效 调用ShowSoundEffect
    /// </summary>
    protected abstract void DoSoundEffect();

    protected void ShowSoundEffect(string clipName)
    {
        //clipName into clip
        AudioClip clip = new AudioClip();
        m_Audio.PlayOneShot(clip);
    }

    /// <summary>
    /// 设置特效存在的时间
    /// </summary>
    protected abstract void SetEffectDisplayTime();
    
	public void Update()
    {
        if (m_EffectDisplayTime >= 0)  //开始播放了之后
        {
            m_EffectDisplayTime -= Time.deltaTime;
            if (m_EffectDisplayTime <= 0)
            {
                m_Light.enabled = false;
                m_lineRenderer.enabled = false;
            }
        }
    }
}
